<?php 
	class StockPriceData {
		private $ric;
		private $name;
		private $closePrice;
		private $livePrice;
		
		public function getRic() {
			return $this->ric;
		}
		public function getName() {
			return $this->name;
		}
		public function getClosePrice() {
			return $this->closePrice;
		}
		public function getLivePrice() {
			return $this->livePrice;
		}
		
		public function setRic($aRic) {
			$this->ric = $aRic;
		}
		public function setName($aName) {
			$this->name = $aName;
		}
		public function setClosePrice($aClosePrice) {
			$this->closePrice = $aClosePrice;
		}
		public function setLivePrice($aLivePrice) {
			$this->livePrice = $aLivePrice;
		}
		
		//public function __construct() {}
		
		public function __construct($Ric1, $name1, $closePrice1, $livePrice1) {
			//print "In BaseClass constructor\n";
			$this->ric = $Ric1;
			$this->name = $name1;
			$this->closePrice = $closePrice1;
			$this->livePrice = $livePrice1;
			
			return $this;
		}
	}
	
	function getStringBetween($str,$from,$to)
	{
		$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
		return substr($sub,0,strpos($sub,$to));
	}

	// individual data
	function generateStockPriceData($data) {
		echo $data;
		
		$arr = @array(explode(",",$data));
		$arr[0] = @getStringBetween($arr[0], ":\"", "\"'");
		$arr[1] = @getStringBetween($arr[1], ":\"", "\"'");
		$arr[2] = @getStringBetween($arr[2], ":\"", "\"'");
		$arr[3] = @getStringBetween($arr[3], ":\"", "\"'");
		
		$obj = new StockPriceData($arr[0], $arr[1], $arr[2], $arr[3]);
		
		//var_dump($obj);
		
		return $obj;
	}
	
	function generateAllStockPriceData($page) {
		$data = str_replace('[', '', $data);
		$data = str_replace(']', '', $data);
		
		$arrOfAll = array(explode("},{", $page));
		
		$arrOfStockPriceData;
		
		foreach($arrOfAll as $item) {
			$item = generateStockPriceData($item);
			$arrOfStockPriceData[] = $item;
		}
		
		// no. of all instruments
		echo count($arrOfStockPriceData);
		return $arrOfStockPriceData;
	}
	
	$baseUrl = 'http://128.199.213.71:9292/';
	
	//no market info
	// return the page content
	function getInstruments($url, $isGettingAllQuotes, $stock) {
		$ch = curl_init();
		if($isGettingAllQuotes == TRUE) {
			//$base = $url;
			curl_setopt ($ch, CURLOPT_URL, $url.'instruments');
		} else {
			curl_setopt ($ch, CURLOPT_URL, $url.'instrument/'.$stock);
		}
		
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
		$jobj = curl_exec($ch);

		//echo $jobj;
		
		if($jobj == FALSE) {
			echo 'market is closed    n empty<br />';
			return 'market is closed    n empty<br />';
		} else {
		// testing
			echo $jobj;
			
			if(empty($jobj)) {
				echo 'no market info  n not empty';
				return 'no market info   n not empty<br />';
			}
			//echo $obj->{"b"};
			
			
			//if(@empty($jobj) || (@strpos($jobj,'{') != false and @strpos($jobj,'{') != false)) {
				//echo 'no market info';
				//return 'no market info <br />';
			//}
			
			if(strpos($jobj, 'is closed') == TRUE) {
				return 'market is close';
			}
			
			$jobj = json_decode($jobj);
			var_dump($jobj);
		}
		return $jobj;	// class of stockPriceData
	}
	
	/*
	var_dump(
		generateAllStockPriceData(
			getInstruments('http://128.199.213.71:9292/', TRUE, '')
		)
	);
	*/
	
	
	//getInstruments($baseUrl, FALSE, '0005.HK');
	
	var_dump(
		getInstruments($baseUrl, TRUE, '')	
		);
	
	
	//$obj = json_decode($json);
?>